
//
//  SpacePhotoView.m
//  cutter
//
//  Created by ZhangLi on 16/1/11.
//  Copyright © 2016年 e360. All rights reserved.
//
#import "AppData.h"
#import "SpacePhotoView.h"
#import "TMUtils.h"
#import "SpaceCustom.h"


//--------------------------------------------------------------------------------------------------
@implementation ZoomView
@synthesize contentView;
-(id)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if (self) {
        contentView=[[UIView alloc] initWithFrame:self.bounds];
        [self addSubview:contentView];
        //
        [self setShowsHorizontalScrollIndicator:NO];
        [self setShowsVerticalScrollIndicator:NO];
        [self setMaximumZoomScale:3.0];
        [self setMinimumZoomScale:1.0];
        [self setDelegate:self];
    }
    return self;
}
//
-(void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    [self setZoomScale:1.0];
    [contentView setFrame:self.bounds];
}
//
-(void)scrollViewDidZoom:(UIScrollView *)scrollView{
    if (self.zoomScale<=1.0) {
        CGFloat cx=CGRectGetMidX(self.bounds);
        CGFloat cy=CGRectGetMidY(self.bounds);
        [contentView setCenter:CGPointMake(cx, cy)];
    }
}
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
    if (scale<=1.0) {
        CGFloat cx=CGRectGetMidX(self.bounds);
        CGFloat cy=CGRectGetMidY(self.bounds);
        [contentView setCenter:CGPointMake(cx, cy)];
    }
}
-(UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return contentView;
}
//可点击
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [[self nextResponder] touchesBegan:touches withEvent:event];
    [super touchesBegan:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [[self nextResponder] touchesMoved:touches withEvent:event];
    [super touchesMoved:touches withEvent:event];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [[self nextResponder] touchesEnded:touches withEvent:event];
    [super touchesEnded:touches withEvent:event];
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    [[self nextResponder] touchesCancelled:touches withEvent:event];
    [super touchesCancelled:touches withEvent:event];
}
@end

//--------------------------------------------------------------------------------------------------

@implementation SpacePhotoCell
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    self=[super initWithCoder:aDecoder];
    if (self) {
        zoomView=[[ZoomView alloc]initWithFrame:self.bounds];
        self.imageView=[[UIImageView alloc] initWithFrame:zoomView.bounds];
        [self addSubview:zoomView];
        [zoomView addSubview:self.imageView];
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    [zoomView setFrame:self.bounds];
    [self.imageView setFrame:zoomView.bounds];
}
-(void)setSource:(id)source{
    
}
@end
//

@implementation SpacePhotoView
-(void)viewDidLoad {
    [super viewDidLoad];
    [[self.pageView layer]setCornerRadius:8.0];
    self.pageView.frame = CGRectMake(self.pageView.frame.origin.x, self.pageView.frame.origin.y, self.view.frame.size.width, self.pageView.frame.size.height);
    NSLog(@"%@",self.data);
}
//UICollectionView
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    int page=(scrollView.contentOffset.x+scrollView.bounds.size.width*0.5)/scrollView.bounds.size.width;
    self.pageView.currentPage=page;
}
- (IBAction)backTouch:(id)sender {
    if(self.navigationController){
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSInteger total = [self.data count];
    [self.pageView setNumberOfPages:total];
    CGSize size = [self.pageView sizeForNumberOfPages:total];
    self.pageWidth.constant = size.width+15;
    return total;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    SpacePhotoCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"spaceidentiflter" forIndexPath:indexPath];
    [AppData loadImage:[self.data objectAtIndex:indexPath.row] view:cell.imageView];
    return cell;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
@end
